﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using QiAdmin.Core;
using QiAdmin.Model.Dtos.Input;
using QiAdmin.Service.IService;
using QiAdmin.Service.Service;
using System.Linq;
using QiAdmin.Core.Attributes;
using QiAdmin.Core.Models;

namespace QiAdmin.WebApi.Controllers.System
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SysDictController : ControllerBase
    {
        readonly ITSysDictTypeService _sysDicTypeService;
        readonly ITSysDictDataService _sysDicDataService;
        public SysDictController(ITSysDictTypeService sysDicTypeService, ITSysDictDataService sysDicDataService)
        {
            _sysDicTypeService = sysDicTypeService;
            _sysDicDataService = sysDicDataService;
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult GetTypePageList(TSysDictType_IN input)
        {
            var res = _sysDicTypeService.GetPageList(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult GetDataPageList(TSysDictData_IN input)
        {
            var res = _sysDicDataService.GetPageList(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult GetType(TSysDictType_IN input)
        {
            var res = _sysDicTypeService.Get(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult GetData(TSysDictData_IN input)
        {
            var res = _sysDicDataService.Get(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Add)]
        [HttpPost]
        public ApiJsonResult AddData(TSysDictType_IN input)
        {
            var res = _sysDicTypeService.AddData(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Edit)]
        [HttpPost]
        public ApiJsonResult EditData(TSysDictType_IN input)
        {
            var res = _sysDicTypeService.EditData(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Delete)]
        [HttpPost]
        public ApiJsonResult RemoveData(List<TSysDictType_IN> input)
        {
            var res = _sysDicTypeService.RemoveData(input);
            return ApiJsonResult.Convert(res);
        }

        [Permission(PermissionType.Query)]
        [HttpPost]
        public ApiJsonResult GetDataList(TSysDictData_IN input)
        {
            var res = _sysDicDataService.GetDictCache().Result;
            if (!string.IsNullOrEmpty(input.TypeName))
            {
                res = res.FindAll(s => s.TypeName == input.TypeName);
            }
            return new ApiJsonResult(res);
        }
    }
}
